/*
 * Table.java
 *
 * Copyright (c) 2008 Teamnet. All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Teamnet.
 */
package ro.prjmanager.database.model;

import java.util.List;
import java.util.ArrayList;
import java.io.Serializable;

/**
 * This class maps an database table properties
 */
public class Table implements Serializable {
    //holds the database table name
    private String databaseName;
    //holds the java table name;
    private String javaName;
    //holds the columns from this tables
    private List<Column> columns;
    //true if this table extend other table
    //it means that the primary key of this table is in an relation on
    // OneToOne with other primary key from other table
    private boolean isExtend;
    //holds the primary key column as database
    private String databasePk;
    //class that this class extend
    private String classExtend;
    //holds the primary key java type
    private String pkClass;

    private boolean isExtended;

    private boolean isOracle;

    private static final long serialVersionUID = -241215539384747178L;

    /**
     * transitive attribute must be injected by the user at the pre-generation time
     * true - if the table generation will be consider unidirectional association
     * false - if the table generation will be consider bidirectional association
     *
     * by default there is considering the bidirectional association
     **/
    private boolean unidirectionalAssociation;

    public Table() {
    }

    public Table(String databaseName, String javaName, List<Column> columns) {
        this.databaseName = databaseName;
        this.javaName = javaName;
        this.columns = columns;
    }

    public Table(String databaseName, String javaName) {
        this.databaseName = databaseName;
        this.javaName = javaName;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getJavaName() {
        return javaName;
    }

    public void setJavaName(String javaName) {
        this.javaName = javaName;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    /**
     * This method is an delegate over the columns and it add an column to the table
     * @param column - the column that has to be added
     * @return - true if the the column has added successfully
     */
    public boolean add(Column column) {
        if(columns==null)
            columns=new ArrayList<Column>();
        return getColumn(column.getDatabaseColumnName()) != null || columns.add(column);
    }

    /**
     * This method find an column in this table for a specified column name
     * @param name - The specified column name
     * @return - The Column coresponding to specified column name
     */
    public Column getColumn(String name){
        if(columns==null||columns.isEmpty())
            return null;
        for(Column col:columns){
            if(col.getDatabaseColumnName().equals(name)||col.getJavaColumnName().equals(name))
                return col;

        }
        return null;
    }

    public boolean isUnidirectionalAssociation() {
        return unidirectionalAssociation;
    }

    public void setUnidirectionalAssociation(boolean unidirectionalAssociation) {
        this.unidirectionalAssociation = unidirectionalAssociation;
    }

    /**
     * This method clone an table object
     * @return - Another instance with the same attributes as this
     */
    public Table clone() {
        Table ret=new Table();
        ret.setDatabaseName(databaseName);
        ret.setJavaName(javaName);
        ret.setColumns(columns);
        return ret;
    }

    public boolean isExtend() {
        return isExtend;
    }

    public void setExtend(boolean extend) {
        isExtend = extend;
    }

    public String getDatabasePk() {
        return databasePk;
    }

    public void setDatabasePk(String databasePk) {
        this.databasePk = databasePk;
    }

    public String getClassExtend() {
        return classExtend;
    }

    public void setClassExtend(String classExtend) {
        this.classExtend = classExtend;
    }

    public String getPkClass() {
        return pkClass;
    }

    public void setPkClass(String pkClass) {
        this.pkClass = pkClass;
    }

    public boolean isExtended() {
        return isExtended;
    }

    public void setExtended(boolean extended) {
        isExtended = extended;
    }

    public boolean isOracle() {
        return isOracle;
    }

    public void setOracle(boolean oracle) {
        isOracle = oracle;
    }

    public String toString() {
        StringBuilder ret=new StringBuilder();
        ret.append("Table{" );
        ret.append("databaseName='").append(databaseName).append('\'');
        ret.append(", javaName='").append(javaName).append('\'');
        if(columns!=null&&!columns.isEmpty()){
            ret.append("columns[");
            for(Column col:columns){
               ret.append("\n");
                ret.append("Columns{");
                ret.append(col.toString());
                ret.append("}");
            }
            ret.append("]");

        }



        return ret.toString();
    }
}
